<?php
/**
 * Created by PhpStorm.
 * User: W
 * Date: 2018/12/4
 * Time: 9:02
 */

namespace app\sports_admin\service;
use app\sports_admin\model\CustomerSchool as CustomerSchoolModel;
use app\sports_admin\model\Customer;
use think\Config;
use think\Request;
use app\sports_admin\model\School as SchoolModel;
use app\exception\SchoolException;
class CustomerSchool
{
    //查询学校业务的备注信息
    public static function selCustomerSchool($id)
    {
        return $data =  CustomerSchoolModel::where('id',$id)->field('remark')->find();
    }
    //
    public static function customerInfo($id)
    {
        return Customer::where('id',$id)->field("id,name")->find();
    }
    //查询学校名 学校地址  代理人id  代理人姓名 然后统一返回给添加学校页面
    public static function customerSchoolMessage($id)
    {
        return CustomerSchoolModel::with("getCustomer,getRegion")->where("id",$id)->find();
    }
    //正式与学校合作
    public static function addCustomerSchool()
    {
        $data = Request::instance()->only(['id','school_name','login','customer_id','phone','password','phone_name','email','url','end','province','address']);
        //判断账号是否被注册过
        if(SchoolModel::where('phone',$data['phone'])->find()){
            throw new SchoolException([
                'msg'=>'手机号已存在',
                'errorCode'=>400
            ]);
        }
        if(SchoolModel::Where('login',$data['login'])->find()){
            throw new SchoolException([
                'msg'=>'账号已存在',
                'errorCode'=>400
            ]);
        }

        $city = $data['province'];
        $data['province'] = intval($city[0]) ? intval($city[0]) : '';
        $data['city'] = intval($city[1]) ? intval($city[1]) : '';
        $data['district'] = intval($city[2])?intval($city[2]) : '';
        $key              = Config::get('schoolPwd');
        $data['password'] = authcode(trim($data['password']),'ENCODE',$key['key'],0);
        $data['end'] = strtotime($data['end']);

        //先把 合作进度表的  状态改为1
        CustomerSchoolModel::where('id',$data['id'])->update(["state"=>1]);
        unset($data['id']);

        $schoolId = SchoolModel::customerSchoolInsert($data);
        if(!$schoolId){
            return json([
                'msg'=>'学校信息添加失败',
                'errorCode'=>400
            ]);
        }return json(['code'=>200,'msg'=>'添加成功']);
    }



}